﻿#include "CameraManager.h"



/** @brief 没啥用，照官方文档抄过来的 */
void CALLBACK g_ExceptionCallBack2(DWORD dwType, LONG lUserID, LONG IHandle, void *pUser){

    char tempbuf[256] = {0};
    switch(dwType){
    case EXCEPTION_RECONNECT:
        printf("--- reconnect ---%d\n", time(NULL));
        break;
    default:
        break;
    }

}

CameraManager::CameraManager()
{

    // 初始化ImageProvider
    _provider1 = QSharedPointer<CameraImageProvider>(new CameraImageProvider);
    _provider2 = QSharedPointer<CameraImageProvider>(new CameraImageProvider);
    _provider4 = QSharedPointer<CameraImageProvider>(new CameraImageProvider);

    gQmlEngine->addImageProvider("CameraShifting", _provider1.data());
    gQmlEngine->addImageProvider("CameraPractice", _provider2.data());
    gQmlEngine->addImageProvider("CameraPurify",   _provider4.data());


    // 初始化HK相机环境
    NET_DVR_Init();
    NET_DVR_SetConnectTime(2000, 1);
    NET_DVR_SetReconnect(10000, true);
    NET_DVR_SetExceptionCallBack_V30(0, NULL, g_ExceptionCallBack2, NULL);


}


/** @brief 单例函数 */
CameraManager& CameraManager::I(){
    static CameraManager instance;
    return instance;
}

void CameraManager::onCameraRefresh(QString cameraName){
    emit sigRefreshCameraImage(cameraName);
}



bool CameraManager::addCamera(QString cameraName, QString ip, int port, QString usrname, QString pswd, QString cameraType){

    if(_cameraList.contains(cameraName)){
        qDebug() << "相机" << cameraName << "已注册，正在删除该相机";
        deleteCamera(cameraName);
        qDebug() << "删除成功";
    }

    qDebug() << "创建新相机" << cameraName << cameraType;
    QSharedPointer<CameraItem>  pCamera;
    if(cameraType == "HIK"){
        pCamera = QSharedPointer<HKCameraItem>(new HKCameraItem(cameraName, ip, port, usrname, pswd));
    }else{
        pCamera = QSharedPointer<JFCameraItem>(new JFCameraItem(cameraName, ip, port, usrname, pswd));
    }
//    QSharedPointer<HKCameraItem> hkCamera = QSharedPointer<HKCameraItem>(new HKCameraItem(cameraName, ip, port, usrname, pswd));


    if(cameraName == "CameraShifting"){
        pCamera->setProvider(_provider1);
    }else if(cameraName == "CameraPractice"){
        pCamera->setProvider(_provider2);
    }else if(cameraName == "CameraPurify"){
        pCamera->setProvider(_provider4);
    }


    connect(pCamera.get(), SIGNAL(sigRefresh(QString)), this, SLOT(onCameraRefresh(QString)));


    _cameraList[cameraName] = pCamera;

//    qDebug() << "addCamera完成";

    return true;

}

// 测试用，已弃用
bool CameraManager::initCamera(){

//    CameraProvider* camera1 = new CameraProvider("CameraPurify", "192.168.0.64", 8000, "admin", "bokun@4312");
//    CameraProvider* camera2 = new CameraProvider("CameraShifting", "192.168.0.65", 8844, "admin", "bokun@1243");

//    connect(camera1, SIGNAL(sigRefresh(QString)), this, SLOT(onCameraRefresh(QString)));
//    connect(camera2, SIGNAL(sigRefresh(QString)), this, SLOT(onCameraRefresh(QString)));

//    camera1->connectCamera();
//    camera2->connectCamera();
//    camera1->start();
//    camera2->start();

//    this->addCamera("CameraPurify", "192.168.0.64", 8000, "admin", "bokun@4312");
//    this->addCamera("CameraShifting", "192.168.0.65", 8844, "admin", "bokun@1243");

//    this->connectCamera("CameraPurify");
//    this->connectCamera("CameraShifting");


    return true;
}


bool CameraManager::connectCamera(QString cameraName){


    if(!_cameraList.contains(cameraName)){
        qDebug() << "相机" << cameraName << "未注册，连接失败";
        return false;
    }

//    qDebug() << "连接相机" << cameraName;

    _cameraList[cameraName]->connectCamera();

//    qDebug() << "连接相机完成";

    return _cameraList[cameraName]->getIsConnected();


}

bool CameraManager::startCaptureCamera(QString cameraName){


    // 停止其他相机的捕获
    foreach(QString name, _cameraList.keys()){
        if(name != cameraName){
            stopCaptureCamera(name);
        }
    }

    if(!_cameraList.contains(cameraName)) {
        qDebug() << "相机" << cameraName <<"未注册，启动捕获失败";
        return false;
    }
    if(_cameraList[cameraName]->isRunning()) {
        qDebug() << "相机正在捕获，启动捕获失败";
        return false;
    }
    if(!_cameraList[cameraName]->getIsConnected()){
        qDebug() << "相机未连接，启动捕获失败";
    }

    _cameraList[cameraName]->start();

    return true;
}

bool CameraManager::stopCaptureCamera(QString cameraName){

    if(!_cameraList.contains(cameraName)){  return false; }
    qDebug() << "停止捕获相机" << cameraName << "的图像";
    _cameraList[cameraName]->stop();
    return true;

}

bool CameraManager::deleteCamera(QString cameraName){

    if(!_cameraList.contains(cameraName)){  return false; }
    _cameraList[cameraName]->stop();
    Sleep(50);
    _cameraList[cameraName]->logout();
    Sleep(50);
    _cameraList.remove(cameraName);
    return true;


}








